package com.fangyi.yunmusic.controller;

import com.fangyi.yunmusic.pojo.front.end.FECommentVO;
import com.fangyi.yunmusic.service.CommentService;
import com.fangyi.yunmusic.service.notification.CommentNotificationService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.fangyi.yunmusic.websocket.CommentNotificationEndpoint;
import com.fangyi.yunmusic.websocket.CommentNotificationHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.fangyi.yunmusic.constant.CommentTypeConstant.ALBUM;
import static com.fangyi.yunmusic.constant.CommentTypeConstant.SONG;
import static com.fangyi.yunmusic.constant.CommentTypeConstant.SONGLIST;
@Api("评论控制器")
@RestController
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    private CommentNotificationHandler commentNotificationHandler;

    @ApiOperation(value = "发表评论", httpMethod = "POST", produces = "application/json")
    @ApiResponse(code = 200, message = "success", response = Object.class)
    @PostMapping("/comment/publish")
    public Object publishComment(@ApiParam(name = "feCommentVO", required = true, value = "前端评论VO") @RequestBody FECommentVO feCommentVO){
        commentService.publishComment(feCommentVO);

        // 更新状态，发送通知给收到评论的用户
        List<String> userIds = commentService.getUserIdsFromCommentTarget(feCommentVO);
        commentNotificationHandler.sendNotReadNumToMany(userIds);
        return "OK";
    }

    @PostMapping("/comment/delete")
    public Object publishComment( String  commentId){
            commentService.deleteComment(commentId);
            return "OK";
    }

    @PostMapping("/commentLike")
    public Object thumbsUpComment(String  userId,String  commentId){
            commentService.commentLike(userId,commentId);
            return "OK";
    }
    @PostMapping("/discommentLike")
    public Object thumbsDownComment(String  userId,String  commentId){
        commentService.discommentLike(userId,commentId);
        return "OK";
    }
    @GetMapping("/iscommentLike")
    public Object isCommentlike(String userId,String commentId){
        return commentService.iscommentLiked(userId,commentId);
    }


//    pageNum:Integer 页码, pageSize:Integer 每页的条数, songId: String 歌曲的ID
    @GetMapping("/comment/song")
    public Object getSongComment(Integer pageNum,Integer pageSize,String  songId){
           PagedResult pagedResult= commentService.selectCommentVO(pageNum,pageSize,SONG,songId);
            return pagedResult;
    }

    @GetMapping("/comment/playlist")
    public Object getPlaylistComment(Integer pageNum,Integer pageSize,String  playlistId){
       return commentService.selectCommentVO(pageNum,pageSize,SONGLIST,playlistId);

    }

    @GetMapping("/comment/album")
    public Object getAlbumComment(Integer pageNum,Integer pageSize,String  albumId){
        return commentService.selectCommentVO(pageNum,pageSize,ALBUM,albumId);
    }

    @GetMapping("/comment/notRead")
    public Object getNotReadComments(String userId) {
        return commentService.getNotReadComments(userId);
    }
}
